﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Threading;

public partial class Admin_TinTuc : System.Web.UI.Page
{
    Tintuc TT = new Tintuc();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            if (Session["user"] != null)
            {
                GridView5.DataSource = TT.LayTT();
                GridView5.DataBind();
            }
            else
                Response.Redirect("DangNhap.aspx");
        }
    }


    protected void BtnTim_Click(object sender, EventArgs e)
    {
        GridView5.DataSource = TT.TimTT(TextBox1.Text, TextBox2.Text, TextBox3.Text);
        GridView5.DataBind();
    }
   
    protected void GridView5_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView5.PageIndex = e.NewPageIndex;
        GridView5.DataSource = TT.LayTT();
        GridView5.DataBind();
    }
    protected void GridView5_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView5.EditIndex = -1;
        GridView5.DataSource = TT.LayTT();
        GridView5.DataBind();
    }
    protected void GridView5_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView5.EditIndex = e.NewEditIndex;
        GridView5.DataSource = TT.LayTT();
        GridView5.DataBind();
    }

    protected void GridView5_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int ma = int.Parse(GridView5.Rows[e.RowIndex].Cells[1].Text);
        try
        {
            TT.XoaTT(ma);
            Label1.Text = "Tin tức đã được xóa";
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
            throw;
        }
        GridView5.DataSource = TT.LayTT();
        GridView5.DataBind();
    }
    protected void GridView5_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int ma = int.Parse(GridView5.Rows[e.RowIndex].Cells[1].Text);

        TextBox txtTieude = (TextBox)GridView5.Rows[e.RowIndex].Cells[2].Controls[0];
        string tieude = txtTieude.Text;
        TextBox txtTomtat = (TextBox)GridView5.Rows[e.RowIndex].Cells[3].Controls[0];
        string tomtat = txtTomtat.Text;
        TextBox txtNgaydang = (TextBox)GridView5.Rows[e.RowIndex].Cells[4].Controls[0];
        string ngaydang = txtNgaydang.Text;
        TextBox txtNoidung = (TextBox)GridView5.Rows[e.RowIndex].Cells[5].Controls[0];
        string noidung = txtNoidung.Text;
        TextBox txtTacgia = (TextBox)GridView5.Rows[e.RowIndex].Cells[6].Controls[0];
        string tacgia=txtTacgia.Text;
        FileUpload fu = (FileUpload)GridView5.Rows[e.RowIndex].Cells[7].FindControl("FileUpload1");
        string hinh = GridView5.DataKeys[e.RowIndex].Values[1].ToString();
        if (fu.HasFile)
        {
            hinh  = Path.GetFileName(fu.PostedFile.FileName);
            fu.PostedFile.SaveAs(Server.MapPath("../Pictures/HinhTT/"+hinh));
            
        }
        TT.SuaTT(ma, tieude, tomtat, ngaydang, noidung, tacgia, hinh);
        GridView5.EditIndex = -1;
        GridView5.DataSource = TT.LayTT();
        GridView5.DataBind();

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("ThemTT.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //xoa
        Thread.Sleep(2000);
        try
        {
            for (int i = 0; i < GridView5.Rows.Count; i++)
            {
                CheckBox chb = (CheckBox)GridView5.Rows[i].Cells[0].FindControl("CB");

                if (chb.Checked == true)
                {
                    int ma = Convert.ToInt32(GridView5.Rows[i].Cells[1].Text);
                    TT.XoaTT(ma);

                }
            }
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;

        }

        GridView5.DataSource = TT.LayTT();
        GridView5.DataBind();
    }
}
